package cn.flexui.flexuiserver.utils;

import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Base64;

/**
 * @author wanmingyu
 * @create 2024/9/20 22:04
 */
public class URLUtil extends cn.hutool.core.util.URLUtil {

    public static String urlToBase64(String url) {
        // 打开URL连接
        URL urlObj;
        try {
            urlObj = new URL(url);
        } catch (MalformedURLException e) {
            throw new RuntimeException(e);
        }
        try (InputStream inputStream = urlObj.openStream()) {
            // 读取数据到字节数组
            byte[] buffer = inputStream.readAllBytes();
            // Base64编码
            return Base64.getEncoder().encodeToString(buffer);
        } catch (Exception e) {
            throw new RuntimeException("获取失败");
        }
    }
}
